﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeTracker
{
    public partial class Toast : Utility.Windows.Forms.AeroForm
    {
        protected static List<Toast> Toasts = new List<Toast>();
        protected static int VerticalPadding = 5;
        protected static int HorizontalPadding = 5;
        protected static int Timeout = 10000;

        public Toast(string message)
        {
            InitializeComponent();

            this.EnableClientExpand = true;
            this.EnableTransparency = false;
            this.UnMovable = true;

            this.label1.Text = message;

            this.AutomaticSize();
        }

        private void AutomaticSize()
        {
            using (Graphics g = this.CreateGraphics())
            {
                SizeF size = g.MeasureString(this.label1.Text, this.label1.Font, this.Width);
                int h = this.Height;
                int h2 = this.label1.Height;
                float newHeight = size.Height;
                this.Height = (int)Math.Ceiling(size.Height) + 42;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void OnLoad(EventArgs e)
        {
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - HorizontalPadding, Screen.PrimaryScreen.WorkingArea.Height - this.Height - VerticalPadding);

            foreach (Toast toast in Toasts)
            {
                toast.Top -= this.Height + VerticalPadding;
            }

            Toasts.Add(this);

            this.timer1.Start();

            base.OnLoad(e);
        }

        private void Toast_FormClosed(object sender, FormClosedEventArgs e)
        {
            foreach (Toast toast in Toasts)
            {
                if (toast.Handle == this.Handle)
                {
                    break;
                }

                toast.Top += this.Height + VerticalPadding;
            }

            Toasts.Remove(this);
        }
    }
}
